package beans;

//import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class Plant
{
	int x;
	int y;
	int CD;//植物的CD 
	int page;//页数
	int HP;//生命值
	/**
	 * 0是射手,1是向日葵,2是坚果，3是炸弹
	 */
	int type;//植物种类
	/**
	 * 
	 * 1.摇摆,2.攻击4.待清除6显示虚影
	 */
	public int state;//植物有这几种状态
	
	public Plant() 
	{

	}

	public Plant(int type, int x , int y)//生成新植物
	{
		this.type= type;
		this.x=x;
		this.y=y;
		if(type==0)
		{
			peaSwingState();
		}
		if(type==1)
		{
			sunFlowerSwingState();
		}
		if(type==2)
		{
			potatoFullHpState();
		}
		if(type==3)
		{
			bombBigState();
		}
		if(type==4)
		{
			iceSwingState();
		}
		if(type==5)
		{
			pepperBigState();
		}
	}
	
	public Plant(int type, int x , int y,int state) //6虚影
	{
		this.type= type;
		this.x=x;
		this.y=y;
		this.state=state;
		if(type==0)
		{
			peaShadeState();
		}
		if(type==1)
		{
			sunFlowerShadeState();
		}
		if(type==2)
		{
			potatoShadeState();
		}
		if(type==3)
		{
			bombShadeState();
		}
		if(type==4)
		{
			iceShadeState();
		}
		if(type==5)
		{
			pepperBigState();
		}
	}
//============================0.射手区====================================
	private void peaSwingState()//豌豆摇摆状态
	{
		HP=300;//豌豆血量为300
		state=1;//1为摇摆状态
	}
	
	private void paintPeaSwing(Graphics g)//绘制豌豆摇摆状态
	{
		Image image = (new ImageIcon("植物大战僵尸/植物/豆瓣/Peashooter/Frame"+page+".png")).getImage();
		g.drawImage(image,34 +x, 81+y, null);//绘制图片API
	}
	
	private void peaSwingActivity(ArrayList <Zombie> zombieList)//射手摇摆时可能的事件
	{
		if(page==12)
		{
			page=0;
		}
		else
		{
			page++;
		}
		
		if(isPeaSeeEveryZombie(zombieList))
		{
			peaAttackState();
		}
		
		if( HP<=0 )
		{
			clearPeaState();
		}
	}

	private void peaAttackState()//射手攻击状态
	{
		state =2 ;
	}
	
	private void peaAttackActivity(ArrayList<Zombie> zombieList,ArrayList<Bullet> bulletList)//射手攻击活动
	{
		if(page==12)
		{
			page=0;
		}
		else
		{
			page++;
		}
		
		if( HP<=0 )
		{
			clearPeaState();
		}
		
		peaCdAdd();
		if(isPeaCdFull())
		{
			//造子弹
			Bullet newBullet = new Bullet(x,y);
			
			//装入容器
			bulletList.add(newBullet);
			peaCdChangeZero();//射手CD初始值为0
		}
		
	}
	
	private void paintPeaAttack(Graphics g)//绘制射手攻击
	{
		Image image = (new ImageIcon("植物大战僵尸/植物/豆瓣/Peashooter/Frame"+page+".png")).getImage();
		g.drawImage(image,34 +x, 81+y, null);//绘制图片API
	}
	
	
	private void clearPeaState()//清理射手状态
	{
		state=4;
	}
	
	void paintPea(Graphics g)//绘制豌豆的两种状态之一
	{
		
		if(state==1)	
		{
			paintPeaSwing(g);
		}
		if(state==2)	
		{
			paintPeaAttack(g);
		}
	}
	
	public void peaActivity(ArrayList<Zombie> zombieList,ArrayList<Bullet> bulletList)
	{
		if(state==1)
		{
			peaSwingActivity(zombieList);
		}
		else if(state==2)
		{
			peaAttackActivity( zombieList,bulletList);
		}

	}
	
	private void peaShadeState()//射手虚影的状态
	{
		state=6;
	}
	
	public void paintPeaShade(Graphics g)//画射手的虚影
	{
		if(state==6)
		{
			Image imageOne = (new ImageIcon("植物大战僵尸/植物/豆瓣/Peashooter/Frame0.png")).getImage();
			g.drawImage(imageOne,x, y, null);//绘制图片API
			Image imageTwo = (new ImageIcon("植物大战僵尸/卡片/准备种植/豆瓣.png")).getImage();
			g.drawImage(imageTwo,34+((x-34)/80)*80, 81+((y-81)/100)*100, null);//绘制图片API
		}
	}
	


	public boolean isPeaSeeEveryZombie(ArrayList<Zombie> zombieList)//豌豆是否有看到任何一只僵尸
	{
		for(  int zombieAmount= 0 ;zombieAmount<= zombieList.size()-1; zombieAmount++)//遍历所有僵尸
		{
			Zombie zombie = zombieList.get(zombieAmount);
			
			if(new Rectangle(x+80, y+50, 720-x, 30).intersects(zombie.x, zombie.y, 80, 100))
			{
				return true;
			}
		}
		return false;
	}

	public void peaCdAdd()//射手CD增加
	{
		CD++;
	}

	public boolean isPeaCdFull()//射手CD满了吗
	{
		return CD>=50;
	}

	public void peaCdChangeZero()//射手的CD改为0：CD为0，再次蓄力
	{
		CD=0;
	}
	//===========================1.向日葵区====================================
	private void sunFlowerSwingState()//向日葵摇摆状态
	{
		state=1;
		HP=300;
	}
	
	private void paintSunFlowerSwing(Graphics g)//绘制太阳花摇摆图
	{
		Image image = (new ImageIcon("植物大战僵尸/植物/向日葵/SunFlower/Frame"+page+".png")).getImage();
		g.drawImage(image,34 +x, 81+y, null);//绘制图片API
	}
	
	private void sunFlowerSwingActivity(ArrayList<Sun> sunList)//太阳花的活动
	{
		if(page==17)
		{
			page=0;
		}
		else
		{
			page++;
		}
			
		if( HP<=0 )
		{
			clearSunFlowerState();
		}
		sunFlowerCdAdd();
		if(isSunFlowerCdFull())
		{
			//造阳光
			Sun newSun = new Sun(x,y);
			
			//装入容器
			sunList.add(newSun);//把新生成的阳光装入数组集合
			sunFlowerCdChangeZero();
		}
	}

	private void clearSunFlowerState()//移除向日葵的状态
	{
		state=4;		
	}

	
	void paintSunFlower(Graphics g)//画向日葵，只有一种 摇摆状态
	{
		if(state==1)	
		{
			paintSunFlowerSwing(g);
		}
	}
	
	public void sunFlowerActivity(ArrayList<Sun> sunList)//太阳花的活动，只能进行摇摆运动
	{
		if(state==1)
		{
			sunFlowerSwingActivity(sunList);
		}
	}
	
	private void sunFlowerShadeState()//向日葵虚影状态
	{
		state=6;
	}
	
	public void paintSunFlowerShade(Graphics g)//绘制太阳花虚影
	{
		if(state==6)
		{
			Image imageOne = (new ImageIcon("植物大战僵尸/植物/向日葵/SunFlower/Frame0.png")).getImage();
			g.drawImage(imageOne,x, y, null);//绘制图片API
			Image imageTwo = (new ImageIcon("植物大战僵尸/卡片/准备种植/向日葵.png")).getImage();
			g.drawImage(imageTwo,34+((x-34)/80)*80, 81+((y-81)/100)*100, null);//绘制图片API
		}
	}
	
	public void sunFlowerCdAdd()//太阳花蓄力
	{
		CD++;
	}

	public boolean isSunFlowerCdFull()//太阳花蓄力满了吗
	{
		return CD>=50;
	}

	public void sunFlowerCdChangeZero()//太阳花CD改为0
	{
		CD=0;
	}
	//===========================2.坚果区====================================
	private void potatoFullHpState()//坚果满血状态
	{
		state=1;
		HP=500;
	}
	
	private void paintPotatoFullHp(Graphics g)//显示坚果满血
	{
		Image image = (new ImageIcon("植物大战僵尸/植物/坚果/WallNut/Frame"+page+".png")).getImage();
		g.drawImage(image,34 +x, 81+y, null);//绘制图片API
	}
	
	private void potatoFullHpActivity()//坚果满血的活动
	{
		if(page==15)
		{
			page=0;
		}
		else
		{
			page++;
		}
			
		if( HP<=300 )
		{
			potatoInjureState();
		}
	}

	private void potatoInjureState()//坚果受伤的状态
	{
		state=2;
		page=0;
	}
	
	private void paintPotatoInjure(Graphics g)//绘制坚果受伤的图
	{
		Image image = (new ImageIcon("植物大战僵尸/植物/坚果/Wallnut_cracked1/Frame"+page+".png")).getImage();
		g.drawImage(image,34 +x, 81+y, null);//绘制图片API
	}
	
	private void potatoInjureActivity()//坚果受伤可能的事件
	{
		if(page==10)
		{
			page=0;
		}
		else
		{
			page++;
		}
			
		if( HP<=150 )
		{
			potatoSeriousState();
		}
	}
	
	
	private void potatoSeriousState()//坚果重伤的状态
	{
		state=3;
		page=0;
	}
	
	private void paintPotatoSerious(Graphics g)//绘制坚果重伤的图
	{
		Image image = (new ImageIcon("植物大战僵尸/植物/坚果/Wallnut_cracked2/Frame"+page+".png")).getImage();
		g.drawImage(image,34 +x, 81+y, null);//绘制图片API
	}
	
	private void potatoSeriousActivity()//坚果重伤后可能遇到的情况
	{
		if(page==14)
		{
			page=0;
		}
		else
		{
			page++;
		}
		if( HP<=0 )
		{
			clearPotatoState();
		}
	}
	
	private void clearPotatoState()//坚果待清除：死亡状态
	{
		state=4;		
	}

	
	void paintPotato(Graphics g)//绘制坚果
	{
		if(state==1)//坚果满血
		{
			paintPotatoFullHp(g);
		}
		if(state==2)//坚果受伤
		{
			paintPotatoInjure(g);
		}
		if(state==3)//坚果重伤
		{
			paintPotatoSerious(g);
		}
	}
	
	public void potatoActivity()//土豆活动
	{
		if(state==1)//土豆满血活动
		{
			potatoFullHpActivity();
		}
		if(state==2)//土豆受伤活动
		{
			potatoInjureActivity();
		}
		if(state==3)//土豆重伤活动
		{
			potatoSeriousActivity();
		}
		
	}
	
	private void potatoShadeState()//土豆虚影状态
	{
		state=6;
	}
	
	public void paintPotatoShade(Graphics g)//坚果显示虚影
	{
		if(state==6)
		{
			Image imageOne = (new ImageIcon("植物大战僵尸/植物/坚果/WallNut/Frame0.png")).getImage();
			g.drawImage(imageOne,x, y, null);
			Image imageTwo = (new ImageIcon("植物大战僵尸/卡片/准备种植/坚果.png")).getImage();
			g.drawImage(imageTwo,34+((x-34)/80)*80, 81+((y-81)/100)*100, null);
		}
	}
	
	//===========================3.炸弹区====================================
	private void bombBigState()//炸弹变大的状态
	{
		page=0;
		state=1;//炸弹变大就是初始状态
		HP=500;//炸弹不要死
	}
	
	private void paintBombBig(Graphics g)//绘制炸弹变大
	{
		Image image = (new ImageIcon("植物大战僵尸/植物/爆炸草莓/CherryBomb/Frame"+page+".png")).getImage();
		g.drawImage(image,34 +x, 81+y, null);//绘制图片API
	}
	
	private void bombBigActivity()//炸弹变大后的活动：可能的事件
	{
		if(page==6)//第六帧的时候爆炸
		{
			bombBombState();
		}
		else
		{
			page++;
		}
			
	}

	private void bombBombState()//炸弹爆炸的状态
	{
		state=2;
	}
	
	private void paintBombBomb(Graphics g)//绘制炸弹爆炸
	{
		Image tu = (new ImageIcon("植物大战僵尸/植物/爆炸草莓/Boom.gif")).getImage();
		g.drawImage(tu,x-33, 51+y, null);
	}
	
	private void bombBombActivity(ArrayList<Zombie> zombieList)//炸弹爆炸后可能的情况
	{
		CD++;
		for( int zombieAmount= 0 ; zombieAmount<= zombieList.size()-1; zombieAmount++)//循环遍历所有僵尸
		{
			Zombie zombie = zombieList.get(zombieAmount);
			if(isBombPointZombie(zombie)) //炸弹是否有碰到僵尸？
			{
				zombie.state=5;
			}
		}
		if(CD == 3 ) 
		{
			clearBombState();
		} 
	}
	
	

	
	private boolean isBombPointZombie(Zombie mouse)//是否炸到One僵尸 炸到这只僵尸了吗
	{
		if(new Rectangle(x-33, 51+y, 240, 300).intersects(mouse.x+34, mouse.y+81, 80, 100))
		{
			return true;
		}
		return false;
	}
	private void clearBombState()//移除炸弹状态
	{
		state=4;		
	}

	
	void paintBomb(Graphics g)
	{
		if(state==1)
		{
			paintBombBig(g);
		}
		if(state==2)
		{
			paintBombBomb(g);
		}

	}
	
	public void bombActivity(ArrayList<Zombie> zombieList)//炸弹的活动
	{
		if(state==1)
		{
			bombBigActivity();
		}
		if(state==2)
		{
			bombBombActivity(zombieList);
		}
		
	}
	
	private void bombShadeState()//炸弹虚影状态
	{
		state=6;
	}
	
	public void paintBombShade(Graphics g)//炸弹显示虚影
	{
		if(state==6)
		{
			Image imageOne = (new ImageIcon("植物大战僵尸/植物/爆炸草莓/CherryBomb/Frame0.png")).getImage();
			g.drawImage(imageOne,x, y, null);//绘制图片API
			Image imageTwo = (new ImageIcon("植物大战僵尸/卡片/准备种植/爆炸草莓.png")).getImage();
			g.drawImage(imageTwo,34+((x-34)/80)*80, 81+((y-81)/100)*100, null);//绘制图片API
		}
	}
	//=========================寒冰射手区==========================
		private void iceSwingState()//豌豆摇摆状态
		{
			HP=300;//豌豆血量为300
			state=1;//1为摇摆状态
		}
		
		private void paintIceSwing(Graphics g)//绘制豌豆摇摆状态
		{
			Image image = (new ImageIcon("植物大战僵尸/植物/冰冻豆瓣/SnowPea/Frame"+page+".png")).getImage();
			g.drawImage(image,34 +x, 81+y, null);//绘制图片API
		}
		
		private void iceSwingActivity(ArrayList <Zombie> zombieList)//射手摇摆时可能的事件
		{
			if(page==12)
			{
				page=0;
			}
			else
			{
				page++;
			}
			
			if(isPeaSeeEveryZombie(zombieList))
			{
				iceAttackState();
			}
			
			if( HP<=0 )
			{
				clearIceState();
			}
		}

		private void iceAttackState()//射手攻击状态
		{
			state =2 ;
		}
		
		private void iceAttackActivity(ArrayList<Zombie> zombieList,ArrayList<Bullet> bulletList)//射手攻击活动
		{
			if(page==12)
			{
				page=0;
			}
			else
			{
				page++;
			}
			
			if( HP<=0 )
			{
				clearIceState();
			}
			
			iceCdAdd();
			if(isIceCdFull())
			{
				//造子弹
				Bullet newBullet = new Bullet(x,y);
				
				//装入容器
				bulletList.add(newBullet);
				iceCdChangeZero();//射手CD初始值为0
			}
			
		}
		
		private void paintIceAttack(Graphics g)//绘制寒冰攻击
		{
			Image image = (new ImageIcon("植物大战僵尸/植物/冰冻豆瓣/SnowPea/Frame"+page+".png")).getImage();
			g.drawImage(image,34 +x, 81+y, null);//绘制图片API
		}
		
		
		private void clearIceState()//清理寒冰状态
		{
			state=4;
		}
		
		void paintIce(Graphics g)//绘制寒冰的两种状态之一
		{
			
			if(state==1)	
			{
				paintIceSwing(g);
			}
			if(state==2)	
			{
				paintIceAttack(g);
			}
		}
		
		public void iceActivity(ArrayList<Zombie> zombieList,ArrayList<Bullet> bulletList)
		{
			if(state==1)
			{
				iceSwingActivity(zombieList);
			}
			else if(state==2)
			{
				iceAttackActivity( zombieList,bulletList);
			}
		}
		
		private void iceShadeState()//寒冰虚影的状态
		{
			state=6;
		}
		
		public void paintIceShade(Graphics g)//画寒冰的虚影
		{
			if(state==6)
			{
				Image imageOne = (new ImageIcon("植物大战僵尸/植物/冰冻豆瓣/SnowPea/Frame0.png")).getImage();
				g.drawImage(imageOne,x, y, null);//绘制图片API
				Image imageTwo = (new ImageIcon("植物大战僵尸/卡片/准备种植/冰冻豆瓣.png")).getImage();
				g.drawImage(imageTwo,34+((x-34)/80)*80, 81+((y-81)/100)*100, null);//绘制图片API
			}
		}
		
		public boolean isIceSeeEveryZombie(ArrayList<Zombie> zombieList)//寒冰是否有看到任何一只僵尸
		{
			for(  int zombieAmount= 0 ;zombieAmount<= zombieList.size()-1; zombieAmount++)//遍历所有僵尸
			{
				Zombie zombie = zombieList.get(zombieAmount);
				
				if(new Rectangle(x+80, y+50, 720-x, 30).intersects(zombie.x, zombie.y, 80, 100))
				{
					return true;
				}
			}
			return false;
		}

		public void iceCdAdd()//寒冰CD增加
		{
			CD++;
		}

		public boolean isIceCdFull()//寒冰CD满了吗
		{
			return CD>=50;
		}

		public void iceCdChangeZero()//寒冰的CD改为0：CD为0，再次蓄力
		{
			CD=0;
		}
		//===========================火爆辣椒区====================================
			private void pepperBigState()
			{
				page=0;
				state=1;//辣椒变大就是初始状态
				HP=500;//辣椒不要死
			}
			
			private void paintPepperBig(Graphics g)//绘制辣椒变大
			{
				Image image = (new ImageIcon("植物大战僵尸/植物/辣椒/Jalapeno/Frame"+page+".png")).getImage();
				g.drawImage(image,34 +x, 81+y, null);//绘制图片API
			}
			
			private void pepperBigActivity()//辣椒变大后的活动：可能的事件
			{
				if(page==7)//第7帧的时候爆炸
				{
					pepperBombState();
				}
				else
				{
					page++;
				}
			}

			private void pepperBombState()
			{
				state=2;
			}
			
			private void paintPepperBomb(Graphics g)
			{

				Image image = (new ImageIcon("植物大战僵尸/植物/辣椒/JalapenoAttack/JalapenoAttack.gif")).getImage();
				g.drawImage(image,x-33, 51+y, null);//绘制图片API
			}
			
			private void pepperBombActivity(ArrayList<Zombie> zombieList)//辣椒爆炸do活动
			{
				CD++;
				for(  int zombieAmount= 0 ; zombieAmount<= zombieList.size()-1  ; zombieAmount=zombieAmount+ 1   )//循环遍历所有僵尸
				{
					Zombie zombie = zombieList.get(zombieAmount);
					if(isPepperPointZombie(zombie)) //辣椒是否有碰到僵尸？
					{
						zombie.state=5;
					}
				}
				if ( CD == 3  ) 
				{
					clearPepperState();
				} 
			}
			
			

			
			private boolean isPepperPointZombie(Zombie mb)//是否炸到One僵尸 炸到这只僵尸了吗
			{
				if(new Rectangle(x-33, 120+y, 1200, 35).intersects(mb.x+34, mb.y+81, 80, 100))
				{
					return true;
				}
				return false;
			}
			private void clearPepperState()//移除炸弹状态
			{
				state=4;		
			}

			
			void paintPepper(Graphics g)
			{
				if(state==1)
				{
					paintPepperBig(g);
				}
				if(state==2)
				{
					paintPepperBomb(g);
				}

			}
			
			public void pepperActivity(ArrayList<Zombie> jsList)
			{
				if(state==1)
				{
					pepperBigActivity();
				}
				if(state==2)
				{
					pepperBombActivity(jsList);
				}
				
			}
			
//			private void pepperShadeState()//炸弹虚影状态
//			{
//				state=6;
//			}
			
			public void paintPepperShade(Graphics g)//炸弹显示虚影
			{
				if(state==6)
				{
					Image imageOne = (new ImageIcon("植物大战僵尸/植物/辣椒/Jalapeno/Frame0.png")).getImage();
					g.drawImage(imageOne,x, y, null);//绘制图片API
					Image imageTwo = (new ImageIcon("植物大战僵尸/卡片/准备种植/辣椒.png")).getImage();
					g.drawImage(imageTwo,34+((x-34)/80)*80, 81+((y-81)/100)*100, null);//绘制图片API
				}
			}
		//=========================显示区=============================
		
		public void removeShadeState()//虚影移除的状态
		{
			state=4;
		}
		
		public void paintShadeState()//虚影显示的状态
		{
			state=6;
		}
		
		public void paint(Graphics g)//画植物
		{
			if(type==0)//画豌豆
			{
				paintPea(g);
			}
			if(type==1)//画太阳花
			{
				paintSunFlower(g);
			}
			if(type==2)//画土豆
			{
				paintPotato(g);
			}
			if(type==3)//画炸弹
			{
				paintBomb(g);
			}
			if(type==4)//画寒冰
			{
				paintIce(g);
			}
			if(type==5)//画辣椒
			{
				paintPepper(g);
			}
		}
		
		public void activity(ArrayList<Zombie> zombieList,ArrayList<Bullet> bulletList,ArrayList<Sun> sunList)//植物的所有活动
		{
			if(type==0)
			{
				peaActivity(zombieList, bulletList);//豌豆可能和僵尸或者子弹活动
			}
			if(type==1)//太阳花的活动
			{
				sunFlowerActivity(sunList);//向日葵可能和太阳活动
			}
			if(type==2)
			{
				potatoActivity();//坚果自己活动
			}
			if(type==3)
			{
				bombActivity(zombieList);//炸弹可能和僵尸活动
			}
			if(type==4)
			{
				iceActivity(zombieList, bulletList);//寒冰可能和僵尸或者子弹活动
			}
			if(type==5)
			{
				pepperActivity(zombieList);//辣椒可能和僵尸活动
			}
		}
	}
